var starttime = []
var geoDim = []// 地理纬度子集列表
var HB_province = '河北省'
var data_JSON = {}

var reqType = 'complain'
var complain_num_sum = '0'// 投诉数量
var complain_num_crm_sum = '0'// crm投诉数量
var complain_num_eoms_sum = '0'// eoms投诉数量
var distribute_num_sum = '0'
var distribute_num_sum2 = '0'
var complain_num = ''// 投诉数量
var pie_data = []
var pie_data_json = {}
var complaint_scene = []// 场景
var scene_complaint_ratio = []// 投诉比例

var comphotspot_d_list = []// 投诉热点
var compcustomer_d_list = []// 重复投诉客户top详情
var $table2 = $('#line2')
var $table6 = $('#line6')

var note_complain_percent_data = [] // 全球通客户投诉占比
// var param = {}
N.indexHome = N.Class.extend({
    resize: function () {
        // var topheight = $('.container-fluid').height() - $('.content-header').height() - 5;
        var topheight = $('.container-fluid').height() - 20
        var bottomheight = topheight * (1 / 2)
        var boxtop = bottomheight - 120
        var maptop = topheight * (2 / 3) - 60
        var mapbot = topheight * (1 / 3) - 60
        $('.control-size-box').height(boxtop)
        $('.control-size-box-map').height(maptop)
        $('.control-size-box-mapbot').height(mapbot)

        var headheight = $('#table1_wrapper .dataTables_scrollHeadInner').height()
        if (headheight) {
            this.tablbeHeight = boxtop - headheight - 5
            $('#table1_wrapper .dataTables_scrollBody').height(this.tablbeHeight)
        }
        headheight = $('#table2_wrapper .dataTables_scrollHeadInner').height()
        if (headheight) {
            this.tablbeHeight = boxtop - headheight - 5
            $('#table2_wrapper .dataTables_scrollBody').height(this.tablbeHeight)
        }
    },
    pageLoad: function (geography, shiduan, geo_dim, city, area) {
        loading.show()
        if (geography) {
            this.geographyChart(shiduan, geo_dim, city, area)
        } else {
            this.timeChart(shiduan, geo_dim, city, area)
        }
    },
    chartTop: function (shiduan, geo_dim, city, area) {
        if (geo_dim === '省') {
            param = {
                geo_dim: geo_dim,
                time_dim: shiduan,
                area: area
            }
        } else {
            param = {
                geo_dim: geo_dim,
                time_dim: shiduan,
                city: city,
                area: area
            }
        }
        service.get(getApiPath() + reqType + '/geographyChart',
            {
                params: param
            }
        ).then(function (res) {
            res = res.data
            setBaseVal(res)
            loading.hide()
            return true
        }).catch(function (e) {
            return false
        })
    },
    geographyChart: function (shiduan, geo_dim, city, area) {
        if (geo_dim === '省') {
            param = {
                geo_dim: city && city != '全量小区' ? '地市' : geo_dim,
                // geo_dim: city && city != '全量小区' ? '地市' : (!city ? '地市' : geo_dim),
                time_dim: shiduan,
                area: area
            }
        } else if (geo_dim === '地市' || geo_dim === '区县') {
            param = {
                geo_dim: area != '全量小区' ? '区县' : geo_dim,
                time_dim: shiduan,
                city: city,
                area: area != '全量小区' ? area : ''
            }
        }
        getWideData(param, reqType + '/geographyChart')
    },
    timeChart: function (shiduan, geo_dim, city, area) {
        if (geo_dim === '省') {
            param = {
                geo_dim: geo_dim,
                time_dim: shiduan,
                area: area,
                num: 10
            }
        } else {
            param = {
                geo_dim: geo_dim,
                time_dim: shiduan,
                city: city,
                area: area,
                num: 10
            }
        }
        getWideData(param, reqType + '/timeChart')
    }
})
var page = new N.indexHome()
$(function () {
    loading.show()

    $(document).on('map:click', function (event, geography, shiduan, geo_dim, map_city, map_area) {
        page.pageLoad(geography, shiduan, geo_dim, map_city, map_area)
    })
})

function cleanRes () {
    starttime = []
    geoDim = []// 地理纬度子集列表

    complain_num_sum = '0'// 投诉数量
    complain_num_crm_sum = '0'// crm投诉数量
    complain_num_eoms_sum = '0'// eoms投诉数量
    distribute_num_sum = '0'
    distribute_num_sum2 = '0'
    complain_num = ''// 投诉数量
    pie_data = []
    pie_data_json = {}

    complaint_scene = []// 场景
    scene_complaint_ratio = []// 投诉比例
    comphotspot_d_list = []
    compcustomer_d_list = []// 重复投诉客户top详情
    $table2.bootstrapTable('destroy')
    $table6.bootstrapTable('destroy')
}

function getWideData (param, reqUrl) {
    console.log('时间维度:' + param.time_dim + '；地理纬度:' + param.geo_dim + '；省:' + HB_province + '；市:' + param.city + '；区县:' + param.area + '；请求地址:' + reqUrl)
    loading.show()
    service.get(getApiPath() + reqUrl,
        {
            params: param
        }
    ).then(function (res) {
        res = res.data
        if (res) {
            cleanRes()
            try {
                setVal(res)
            } catch (e) {
                console.log(e)
            }
        }
        drawPage()
    }).catch(function (e) {
        return false
    })
}

function drawPage () {
    // 投诉量 投诉类型占比
    data_JSON = {
        id: 'line1',
        title: '类型',
        type: 'variablepie',
        align: 'right',
        verticalAlign: 'middle',
        titleX: -70,
        data: pie_data,
        marginTop: 30,
        marginBottom: 0,
        itemWidth: 110,
        lenabled: true,
        lenabledAlign: 'center',
        lenabledVerticalAlign: '',
        layout: 'vertical',
        lenabledWidth: 10,
        chartType: 'column'

    }
    drawPie(data_JSON, callBack)
    // 本月累计全省集中投诉热点
    $table2.bootstrapTable({
        columns: [
            {field: 'creation_time', title: '投诉时间'},
            {field: 'city', title: '地市'},
            {field: 'complaint_location', title: '投诉地点'},
            {field: 'complaint_num', title: '投诉次数'}
        ],
        data: comphotspot_d_list
    })
    // 场景投诉比例
    data_JSON = {
        id: 'line5',
        title: null,
        cate: complaint_scene,
        data: [
            {
                name: '场景投诉比例(%)',
                type: 'column',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.blue,
                data: scene_complaint_ratio,
                yAxis: 0
            }
        ],
        marginTop: 30,
        marginBottom: 0,
        itemWidth: 110,
        lenabled: false,
        lenabledAlign: 'center',
        lenabledVerticalAlign: '',
        layout: 'horizontal',
        lenabledWidth: 10,
        chartType: 'column',
        inverted: true

    }
    // drawColumnCol(data_JSON);
    // 昨日全球通客户投诉占比
    data_JSON = {
        id: 'line5',
        title: '占比(%)',
        type: 'variablepie',
        align: 'right',
        verticalAlign: 'middle',
        titleX: -70,
        data: note_complain_percent_data,
        marginTop: 30,
        marginBottom: 0,
        itemWidth: 110,
        lenabled: true,
        lenabledAlign: 'center',
        lenabledVerticalAlign: '',
        layout: 'vertical',
        lenabledWidth: 10,
        chartType: 'column'

    }
    drawPie(data_JSON, callBack)

    // 重复投诉客户top详情

    $table6.bootstrapTable({
        columns: [
            {field: 'creation_time', title: '投诉时间'},
            {field: 'city', title: '地市'},
            {field: 'customer_number', title: '客户号码'},
            {field: 'complaint_num', title: '投诉次数'}
        ],
        data: compcustomer_d_list
    })

    loading.hide()
}

function setVal (item) {
    // starttime.push(item.starttime.substr(4));
    // item.area === '全量小区' ? geoDim.push(item.city) : geoDim.push(item.area);
    for (var i = 0; i < item.comptype_d_list.length; i++) {
        // pie_data_json.y = item.comptype_d_list[i].distribute_num;
        pie_data_json = {y: item.comptype_d_list[i].complain_num, name: item.comptype_d_list[i].distribute_type}
        pie_data.push(pie_data_json)
        complain_num_sum = parseInt(complain_num_sum) + parseInt(item.comptype_d_list[i].complain_num)
        complain_num_crm_sum = parseInt(complain_num_crm_sum) + parseInt(item.comptype_d_list[i].distribute_num_crm)// crm投诉数量
        complain_num_eoms_sum = parseInt(complain_num_eoms_sum) + parseInt(item.comptype_d_list[i].distribute_num_eoms)// eoms投诉数量
    }
    $('#complain_num_sum').html(toThousands(complain_num_sum))
    $('#distribute_num_sum').html(toThousands(complain_num_crm_sum))// crm派发数量
    $('#distribute_num_sum2').html(toThousands(complain_num_eoms_sum))// eoms派发数量

    comphotspot_d_list = item.comphotspot_d_list
    $('#complaints_notice').html(item.widetable_d_list[0].complaints_notice)// 投诉公告
    item.compscene_d_list.filter(function (val) {
        complaint_scene.push(val.complaint_scene)// 场景
        scene_complaint_ratio.push(val.scene_complaint_ratio)// 投诉比例
    })
    compcustomer_d_list = item.compcustomer_d_list
    compcustomer_d_list.filter(function (value) {
        value.customer_number = value.customer_number.substr(0, 3) + '****' + value.customer_number.substr(-4)
    })

    note_complain_percent_data = [
        {y: parseFloat(10.37288.toFixed(2)), name: '全球通-银卡'},
        {y: parseFloat(8.474576.toFixed(2)), name: '全球通-金卡'},
        {y: parseFloat(3.457627.toFixed(2)), name: '全球通-白金卡'},
        {y: parseFloat(1.152542.toFixed(2)), name: '全球通-钻石卡'}
    ]
}

function callBack (c) { // 图表初始化完毕后的会掉函数
    // 环形图圆心
    try {
        var centerY = c.series[0].center[1],
            titleHeight = parseInt(c.title.styles.fontSize)
    } catch (e) {
        console.log(e)
    }
    // 动态设置标题位置
    c.setTitle({
        y: centerY + titleHeight / 4
    })
}
